﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Engine;
using Game;
using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using Game.Server.Plugins;
using GameEntitySystem;
using Newtonsoft.Json;
using static System.Net.Mime.MediaTypeNames;
using Engine.Graphics;
using System.Net.NetworkInformation;
using System.Media;
using TemplatesDatabase;
using Newtonsoft.Json.Linq;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using System.Xml;
using OpenTK;
using LiteNetLib;
using System.Xml.Schema;
using OpenTK.Platform.MacOS;
using System.Linq;
using System.IO.Pipes;
using static Game.MotdManager;
using Vector3 = Engine.Vector3;

namespace Game.Server.Plugins
{
    public class ee1 : ServerPlugin, IBanEventHandle, IMessageEventHandle
    {
        public override int Version => 50000;

        public override string Name => "敏感词管理插件";

        

        public Project Project = new Project();

        public static int 敏感词个数;

        public void Explode(int x, int y, int z, ref float pressure, bool isIncendiary, bool noExplosionSound, PlayerData miner)
        {
            if (LimitCore.DataDictionary.LimitExplode)
            {
                pressure = 0f;
            }
        }

        public  static string 设置文件 = "Plugins\\设置文件.txt";//绝对文件及路径

        public static string path12 = "不文明用语日志.txt";//绝对文件及路径

        public string[] 敏感词库1;

        public byte FirstLevel => 1;

        public static int 达瓦撒 { get; set; } = 1;

        public override void Initialize()
        {
            
            BanEventManager.AddObject(this);
            MessageEventManager.AddObject(this);
            CmdManager.AddProcessCmdObject(new CmdLimit());
            {//设置文件
                DateTime hhh = DateTime.Now;
                string 时间 = "[" + hhh.Year.ToString() + ":" + hhh.Month.ToString() + ":" + hhh.Day.ToString() + ":" + hhh.Hour.ToString() + ":" + hhh.Minute.ToString() + ":" + hhh.Second.ToString() + "]";
                while (!File.Exists(path12))
                {
                    var fileStream = new FileStream("不文明用语日志.txt", FileMode.Create); // 创建文件
                    var 文件写入 = new StreamWriter(fileStream);// 写入文件
                    文件写入.Write(时间 + "启动"); // 写入内容
                    文件写入.Close(); // 关闭文件
                    fileStream.Close(); // 关闭流
                }
                if(!File.Exists(设置文件))
                {
                    var 设置文件e = new FileStream(设置文件, FileMode.Create); // 创建文件
                    var 文件写入 = new StreamWriter(设置文件e); // 写入文件
                    文件写入.Write("sb\n"); // 写入内容
                    文件写入.Close();
                    设置文件e.Close();
                    
                    
                }
            }
            敏感词库();
        }

        public override void Load()
        {
            
        }

        public override void Save()
        {
            
        }

        public void 处罚策士(string playerName, NetNode netNode, Client From, string message, byte messageType, out bool External)
        {
            var rr = new rr();
            External = false;

            Time.QueueTimeDelayedExecution(Time.FrameStartTime + 3.0, delegate () { DisplaySmallMessageNet广播(playerName + "骂" + message + "被警告"); });//延迟执行3秒
            DisplaySmallMessageNet1(From, playerName + "警告一次");
            Time.QueueTimeDelayedExecution(Time.FrameStartTime + 0.99, delegate () { 踢人(From, message); });
            文件写入(From, message);
            
        }

        public void 敏感词库()
        {
            敏感词库1 = File.ReadAllLines(设置文件); // 读取词库
            try
            {
                Time.QueueTimeDelayedExecution(Time.FrameDuration + 10.0, delegate () { Log.Information("使用方法\n"+ "在Plugins/设置文件.txt 路径里写敏感词\n一个换行代表一个敏感词\n /serversensitive 在服务端中操作\n /limit 依照服务端自带插件limit制作"); });
                Log.Information("一共" + 敏感词库1.Count()+"敏感词"); // 输出词库数量
                敏感词个数 = 敏感词库1.Count();
                Log.Information("敏感词库========================");
                int count;
                lock (Project)
                {
                    foreach (string line in 敏感词库1) // 读取词库
                    {

                        Log.Information(line); // 输出词库内容
                    }
                    Log.Information("================================");
                }
            }
            catch
            {
                Log.Information("词库读取失败");
            }
        }
    

        public void DisplayLargeMessageNet(ComponentPlayer componentPlayer, string text, string more, float duration = 2, float delay = 0) // 标题信息方法
        {
            var messagePackget = new MessagePackage(text, more, duration, delay);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void load(ComponentPlayer componentPlayer, string text) // 读取方法
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }

        public void 状态c()
        {
            var rr = new rr();
            达瓦撒 = rr.状态;
        }

        public void DisplaySmallMessageNet(ComponentPlayer componentPlayer, string text)  // 小消息方法
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void 文件写入(Client client, string message)  //写入方法
        {
            DateTime hhh = DateTime.Now;
            string 时间 = "[" + hhh.Year.ToString() + ":" + hhh.Month.ToString() + ":" + hhh.Day.ToString() + ":" + hhh.Hour.ToString() + ":" + hhh.Minute.ToString() + ":" + hhh.Second.ToString() + "]";

            var fileStream3 = new FileStream("不文明用语日志.txt", FileMode.Append, FileAccess.Write);
            var 文件写入3 = new StreamWriter(fileStream3);
            文件写入3.Write("\n" + 时间 + client + " " + "玩家名称:" + "[" + client.PlayerData.m_name + "]" + "玩家tokey:" + "[" + client.TokenId + "]" + message);
            文件写入3.Write("\r IP:" + "[" + client.IPPoint + "]");
            Log.Information(client.IPPoint);
            文件写入3.Close();
        }

        public void DisplaySmallMessageNet1(Client client, string text)  // 小消息方法 From
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = client;
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void DisplaySmallMessageNet广播(string text)  // 小消息方法 广播
        {
            var messagePackget = new MessagePackage("<c=red>系统广播</c>", text, 0, null);
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void 踢人(Client client, string text)  // 小消息方法 广播
        {
            var BanAddPackget = new MessagePackage(null, text, 0, null);
            BanAddPackget.From = client.PlayerData.Client;
            CommonLib.Net.RemoveClient(client, "请不要骂人");

        }

        public bool IsBan(string nickname, string id, string password, string ip, NetNode netNode, Client client, out bool UseExternalPassword)  //在玩家连接服务器时执行，通过昵称或者社区id踢人，返回true则不允许进入
        {
            UseExternalPassword = false;
            return false;
        }

        public bool IsBanIp(string ip, NetNode netNode, ConnectionRequest request)   //在玩家连接服务器时执行，通过ip踢人，返回true则不允许进入
        {

            return false;
        }

        public void ReceiveMessage(string playerName, NetNode netNode, Client From, string message, byte messageType, out bool External)  //接收到聊天信息时执行，message为消息内容
        {
            lock (Project)
            {
                var rr = new rr();
                External = false;
                if (达瓦撒 == 1)
                {

                    foreach (string str in 敏感词库1)
                    {

                        if (message.Contains(str))
                        {
                            处罚策士(playerName, netNode, From, message, messageType, out External);
                        }

                    }

                }
                else if (达瓦撒 == 0)
                {

                }
            }
               
        }

        public bool EditSignMessage(Point3 point, ComponentPlayer componentPlayer)
        {
            return false;
        }


    }
    public class 开关系统 : AbstractProcessCmd
    {
        public int 状态 { get; set; } = 1;

        public override string Cmd
        {
            get
            {
                return "sensitive";
            }
        }

        public override string Introduce
        {
            get
            {
                return "/sensitive    -查看敏感词命令";
            }
        }

        public override int AuthLevel => 10000;

        public override DisplayType Display => 0;


        public void SendMsgPack(ComponentPlayer componentPlayer, string text)  //发送消息方法
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

        public int rr(int 状态1)
        {
            return 状态1;
        }

        private void SendHelpMessage()
        {
            string text = "\n/sensitive off或on";
            base.SendMessage("查看敏感词命令=====>>>", text);
        }

        public override void ProcessCmd()
        {
            bool flag = GameManager.Project == null;
            bool flag2 = flag;
            if (flag2)
            {
                base.SendMessage("敏感词操作器=====>>>", "你需要先开始游戏");
            }
            else
            {
                bool flag3 = false;
                SubsystemPlayers subsystemPlayers = GameManager.Project.FindSubsystem<SubsystemPlayers>();
                ComponentPlayer componentPlayer = this.m_client.PlayerData.ComponentPlayer;
                SubsystemExplosions subsystemExplosions = GameManager.Project.FindSubsystem<SubsystemExplosions>();
                int num = this.m_messageDatas.Length;
                int num2 = num;
                var rr = new rr();
                if (num2 != 2)
                {
                    this.SendHelpMessage();
                }
                else
                {
                    foreach (PlayerData playerData in subsystemPlayers.PlayersData)
                    {
                        DateTime hhh = DateTime.Now;
                        string 时间 = "[" + hhh.Year.ToString() + ":" + hhh.Month.ToString() + ":" + hhh.Day.ToString() + ":" + hhh.Hour.ToString() + ":" + hhh.Minute.ToString() + ":" + hhh.Second.ToString() + "]";



                        bool flag7 = "on" == this.m_messageDatas[1];
                        bool flag6 = "off" == this.m_messageDatas[1];
                        if (flag6)
                        {

                            状态 = 0;
                            rr.状态 = 状态;
                            SendMsgPack(playerData.ComponentPlayer, 状态 + "已关闭");
                            rr.gg();
                            var fileStream3 = new FileStream("不文明用语日志.txt", FileMode.Append, FileAccess.Write);
                            var 文件写入3 = new StreamWriter(fileStream3);
                            文件写入3.Write(时间 + "\n" + 时间 + "已关闭敏感词检测");
                            文件写入3.Close();
                        }
                        else if (flag7)
                        {

                            状态 = 1;
                            int 状态2 = 状态;
                            rr.状态 = 状态;
                            SendMsgPack(playerData.ComponentPlayer, 状态 + "已开启");
                            rr.gg();
                            var fileStream3 = new FileStream("不文明用语日志.txt", FileMode.Append, FileAccess.Write);
                            var 文件写入3 = new StreamWriter(fileStream3);
                            文件写入3.Write("\n" + 时间 + "已开启敏感词检测");
                            文件写入3.Close();
                        }
                    }
                }
            }
        }
    }

    public class rr    //类
    {
        public static int 状态 { get; set; } = 1;

        public void gg()
        {
            var ee1 = new ee1();
            ee1.状态c();
        }

    }
    public class 日志文件 : AbstractProcessCmd
    {
        public int 状态 { get; set; } = 1;

        public override string Cmd
        {
            get
            {
                return "sensitiveinfo";
            }
        }

        public override string Introduce
        {
            get
            {
                return "/sensitiveinfo    -查看敏感词日志命令";
            }
        }

        public override int AuthLevel => 1000;

        public override DisplayType Display => 0;

        private void SendHelpMessage()
        {
            string text = "\n/sensitiveinfo  -查看日志";
            base.SendMessage("查看敏感词命令=====>>>", text);
        }
        public override void Process(string username, NetNode netNode, Client client, string[] messageDatas, bool isTerminal = false)
        {
            base.Process(username, netNode, client, messageDatas, isTerminal);
        }

        public void SendMsgPack(ComponentPlayer componentPlayer, string text)  //发送消息方法
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

        public override void ProcessCmd()
        {

            SubsystemPlayers subsystemPlayers = GameManager.Project.FindSubsystem<SubsystemPlayers>();
            ComponentPlayer componentPlayer = this.m_client.PlayerData.ComponentPlayer;
            SubsystemExplosions subsystemExplosions = GameManager.Project.FindSubsystem<SubsystemExplosions>();
            int num = this.m_messageDatas.Length;
            int num2 = num;
            
            
                bool flag = GameManager.Project == null;
                bool flag2 = flag;
                if (flag2)
                {
                    base.SendMessage("敏感词info操作器=====>>>", "你需要先开始游戏");
                }
                else
                {
                    foreach (PlayerData playerData in subsystemPlayers.PlayersData)
                    {
                        var 开关系统 = new 开关系统();
                        string filePath = "不文明用语日志.txt"; // 替换为你的TXT文件路径  
                        try
                        {
                            // 创建一个StreamReader对象来读取文件  
                            using (StreamReader sr = new StreamReader(filePath))
                            {
                                string line;
                            SendMsgPack(componentPlayer, "日志内容------------------------日志内容");
                            // 逐行读取文件，直到读取完毕  
                            while ((line = sr.ReadLine()) != null)
                                {
                                
                                SendMsgPack(componentPlayer, line);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                        // 处理可能出现的异常，例如文件不存在或没有读取权限等  
                        SendMsgPack(componentPlayer,"读取失败");
                        SendMsgPack(componentPlayer,e.Message);
                        }
                    }
                }
            }
        }
    public class 敏感词的数 : AbstractProcessCmd
    {
        ee1 ee1 = new ee1();

        public static int 累加 { get; set; } = 0;

        public override string Cmd
        {
            get
            {
                return "sensitivecount";
            }
        }

        public override string Introduce
        {
            get
            {
                return "/sensitivecount    -查看敏感词个数命令";
            }
        }

        public override int AuthLevel => 1000;

        public override DisplayType Display => 0;

        private void SendHelpMessage()
        {
            string text = "\n/sensitiveinfo  -查看日志";
            base.SendMessage("查看敏感词命令=====>>>", text);
        }
        public override void Process(string username, NetNode netNode, Client client, string[] messageDatas, bool isTerminal = false)
        {
            base.Process(username, netNode, client, messageDatas, isTerminal);
        }

        public void SendMsgPack(ComponentPlayer componentPlayer, string text)  //发送消息方法
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

        public void SendMsgPack1(string meg,ComponentPlayer componentPlayer, string text)  //不带系统发送消息方法
        {
            MessagePackage messagePackage = new MessagePackage(meg, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }

        public override void ProcessCmd()
        {
            SubsystemPlayers subsystemPlayers = GameManager.Project.FindSubsystem<SubsystemPlayers>();
            ComponentPlayer componentPlayer = this.m_client.PlayerData.ComponentPlayer;
            SubsystemExplosions subsystemExplosions = GameManager.Project.FindSubsystem<SubsystemExplosions>();
            int num = this.m_messageDatas.Length;
            int num2 = num;


            bool flag = GameManager.Project == null;
            bool flag2 = flag;
            if (flag2)
            {
                base.SendMessage("敏感词info操作器=====>>>", "你需要先开始游戏");
            }
            else
            {
                foreach (PlayerData playerData in subsystemPlayers.PlayersData)
                {
                    var 开关系统 = new 开关系统();
                    string filePath = "Plugins\\设置文件.txt"; // 替换为你的TXT文件路径  
                    try
                    {
                        // 创建一个StreamReader对象来读取文件  
                        using (StreamReader sr = new StreamReader(filePath))
                        {
                            string line;
                            SendMsgPack1("<c=red>敏感词系统</c>",componentPlayer, "一共"+ ee1.敏感词个数 + "个敏感词");
                            int r=0;
                            // 逐行读取文件，直到读取完毕  
                            while ((line = sr.ReadLine()) != null)
                            {                                                     
                                    SendMsgPack1("<c=red> ◆》》》 </c>", componentPlayer, line);                               
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // 处理可能出现的异常，例如文件不存在或没有读取权限等  
                        SendMsgPack(componentPlayer, "读取失败");
                        SendMsgPack(componentPlayer, e.Message);
                    }
                }
            }
        }
    }
    public class SERVER : AbstractProcessCmd
    {
        public override string Cmd => "serversensitive";

        public override string Introduce => "敏感词插件";

        public override int AuthLevel => 100;

        public override DisplayType Display => DisplayType.Authority;

        private void SendHelpMessage()
        {
            string message = "\n /serversensitive [on]  打开敏感词检测\n /serversensitive [off]  关闭敏感词检测\n /serversensitive [display]  查看敏感词\n /serversensitive [info]  查看日志\n /serversensitive [down] 关闭服务端\n /serversensitive [cleaninfo] 清除敏感词日志";
            SendMessage("serversensitive", message);
        }

        public override void ProcessCmd()
        {
            int num = this.m_messageDatas.Length;
            if (num != 2)
            {
                SendHelpMessage();
            }
            if (m_messageDatas.Length == 2)
            {
                if (m_messageDatas[1] == "off")
                {
                    var rr = new rr();
                    rr.状态 = 0;
                    rr.gg();
                    SendMessage("serversensitive", "关闭敏感词");
                    return;
                }

                if (m_messageDatas[1] == "on")
                {
                    var rr = new rr();
                    rr.状态 = 1;
                    rr.gg();
                    SendMessage("serversensitive", "开启敏感词");
                    return;
                }
                if (m_messageDatas[1]=="display")
                {
                    string filePath = "Plugins\\设置文件.txt"; // 替换为你的TXT文件路径  
                    try
                    {
                        // 创建一个StreamReader对象来读取文件  
                        using (StreamReader sr = new StreamReader(filePath))
                        {
                            string line;
                            SendMessage("serversensitive", "一共" + ee1.敏感词个数 + "个敏感词");
                            int r = 0;
                            // 逐行读取文件，直到读取完毕  
                            while ((line = sr.ReadLine()) != null)
                            {
                                SendMessage("serversensitive", line);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // 处理可能出现的异常，例如文件不存在或没有读取权限等  
                        SendMessage("serversensitive", "读取失败");
                        SendMessage("serversensitive", e.Message);
                    }
                }
                if(m_messageDatas[1] == "info")
                {
                    string filePath = "不文明用语日志.txt"; // 替换为你的TXT文件路径  
                    try
                    {
                        // 创建一个StreamReader对象来读取文件  
                        using (StreamReader sr = new StreamReader(filePath))
                        {
                            string line;
                            SendMessage("serversensitive info", "日志内容------------------------日志内容");
                            // 逐行读取文件，直到读取完毕  
                            while ((line = sr.ReadLine()) != null)
                            {

                                SendMessage("serversensitive info", line);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // 处理可能出现的异常，例如文件不存在或没有读取权限等  
                        SendMessage("serversensitive info", "读取失败");
                        SendMessage("serversensitive info", e.Message);
                    }

                }
                if(m_messageDatas[1] == "down")
                {
                    Environment.Exit(0);
                }
                if(m_messageDatas[1] == "cleaninfo")
                {
                    string filePath = "不文明用语日志.txt"; // 替换为你的TXT文件路径
                    try
                    {
                        File.WriteAllText(filePath, "");
                        SendMessage("serversensitive info","成功清除");
                    }
                    catch(Exception ex)
                    {
                        SendMessage("serversensitive info", "清除失败");
                        SendMessage("serversensitive info", ex.Message);
                    }
                }
            }
        }     
    }

}


